//
//  SPEditViewController.m
//  Sport
//
//  Created by Cass on 2017/12/3.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "SPEditViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface SPEditViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *iconView;

@property (weak, nonatomic) IBOutlet UIImageView *iconImg;
@property (weak, nonatomic) IBOutlet UIButton *iconBtn;

@end

@implementation SPEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.iconImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alertAction)];
    [self.iconImg addGestureRecognizer:tap];
    [self.iconView bringSubviewToFront:self.iconImg];
//    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//        NSLog(@"打开相机失败");
//
//    }
//    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
//    if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
//        NSLog(@"无法访问相机");
//    }
}

- (void)alertAction{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"选择相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"调用相册");
        [self photo:0];
    }];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"调用相机");
        [self photo:1];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    [alertVC addAction:photoAction];
    [alertVC addAction:cameraAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
- (void)photo:(NSInteger)index{
    UIImagePickerController *imgPicVC = [[UIImagePickerController alloc]init];
    switch (index) {
        case 0:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                imgPicVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                imgPicVC.delegate = self;
                imgPicVC.allowsEditing = YES;
                [self presentViewController:imgPicVC animated:YES completion:nil];
            }
            break;
        case 1:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                imgPicVC.sourceType = UIImagePickerControllerSourceTypeCamera;
                imgPicVC.delegate = self;
                imgPicVC.allowsEditing = YES;
                if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
                    imgPicVC.modalPresentationStyle = UIModalPresentationCurrentContext;
                }
                [self presentViewController:imgPicVC animated:YES completion:nil];
            }
            break;
        default:
            break;
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *img = info[UIImagePickerControllerOriginalImage];
    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        [picker dismissViewControllerAnimated:YES completion:nil];
        [self.iconImg setImage:img];
    }else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
        [picker dismissViewControllerAnimated:YES completion:nil];
        [self.iconImg setImage:img];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
